﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using DataAccessTecnoPez;
using CrystalDecisions.Enterprise;
using System.IO;

public partial class Controls_visorReportes : System.Web.UI.Page
{
    private string selectionFormula;
    private string paraDescript;
    private string paraValor;
    private void Page_Load(object sender, System.EventArgs e)
    {
        //Introducir aquí el código de usuario para inicializar la página
        if (IsPostBack)
        {
            string lastReport=ViewState["lastReport"] as string;
            if(lastReport!=null)
            {
               Response.Redirect(lastReport);
            }

            return;
        }
        selectionFormula = Session["rptSeleccionFormula"].ToString();
        paraDescript = Session["paraDescript"].ToString();
        paraValor = Session["paravalor"].ToString();

     

        ReportClass reporte = Session["urlReport"] as ReportClass;
        if (reporte == null)
        {
            throw new ArgumentException("No se seteo el reporte");
        }
        
        //Limpio porque ya no lo uso
        Session.Remove("paravalor");
        Session.Remove("paraDescript");
        Session.Remove("rptSeleccionFormula");
        Session.Remove("urlReport");
        
        Database crDatabase;
        Tables crTables;
        TableLogOnInfo crTableLogOnInfo;
        ConnectionInfo crConnectionInfo;
        
        
        crConnectionInfo = new ConnectionInfo();


        crConnectionInfo.ServerName = System.Configuration.ConfigurationManager.AppSettings["ServerName"];
        crConnectionInfo.DatabaseName = System.Configuration.ConfigurationManager.AppSettings["DatabaseName"];
        crConnectionInfo.UserID = System.Configuration.ConfigurationManager.AppSettings["UserID"];
        crConnectionInfo.Password = System.Configuration.ConfigurationManager.AppSettings["Password"];
        
        crDatabase = reporte.Database;
        crTables = crDatabase.Tables;
        foreach (CrystalDecisions.CrystalReports.Engine.Table crTable2 in crTables)
        {
            crTableLogOnInfo = crTable2.LogOnInfo;
            crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
            crTable2.ApplyLogOnInfo(crTableLogOnInfo);
            //Puto puto crystal reports!!!!!!!!!!!!
            crTable2.Location = "dbo." + crTable2.Location.Substring(crTable2.Location.LastIndexOf(".") + 1);

        }
        string destino;
        string nombreArch;
        nombreArch = reporte.GetType().Name + User.Identity.Name + ".pdf";

        destino = Server.MapPath(System.DBNull.Value.ToString()) + "\\visorDeReportes\\" + nombreArch;
        string lastReporte=Tecnopez.Globals.Functions.getHost(this) + "Controls/visorDeReportes/" + nombreArch;
        ViewState["lastReport"]= lastReporte;
        Response.AddHeader("Refresh", "2;url=" + lastReporte);
        ExportToPDF(reporte, destino);

    }
    public string ExportToPDF(ReportDocument rpt, string NombreArchivo)
    {
        string vFileName;
        DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
        string[] param;
        string[] valor;
        {
            rpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            rpt.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
        }

        rpt.RecordSelectionFormula = selectionFormula;
        param = paraDescript.Split(Convert.ToChar(","));
        valor = paraValor.Split(Convert.ToChar(","));
        if (param.GetUpperBound(0) != 0)
        {
            for (Int32 i = param.GetLowerBound(0); i <= param.GetUpperBound(0); i++)
            {
                rpt.SetParameterValue(param[i], valor[i]);
            }
        }
        vFileName = NombreArchivo;
        if (System.IO.File.Exists(vFileName))
            System.IO.File.Delete(vFileName);
        diskOpts.DiskFileName = vFileName;
        rpt.ExportOptions.DestinationOptions = diskOpts;
        rpt.Export();


        return vFileName;
    }

}
